package com.sanji.pay.sys.web;

import com.sanji.pay.common.Result;
import com.sanji.pay.common.ResultGenerator;
import com.sanji.pay.sys.domain.Account;
import com.sanji.pay.sys.repository.AccountRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.util.*;
import java.util.function.Supplier;

import static com.sanji.pay.common.ResultGenerator.error;
import static com.sanji.pay.common.ResultGenerator.ok;

/**
 * 描述: 商户restFor
 * 包名: com.sanji.pay.sys.web
 * 作者: ChenGuop.
 * 日期: 2017/09/19 下午1:46.
 * 项目名称: sanji-pay
 * 版本: 1.0
 * JDK: since 1.8
 */
@RestController
@RequestMapping(value = "sys/account"/*, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE*/)
public class AccountController {

  @Autowired
  private AccountRepository repository;

  @Autowired
  private BCryptPasswordEncoder bCryptPasswordEncoder;

  @PostMapping
  @Transactional
  public Result<String> add(@RequestBody Account account){
    if (Objects.isNull(account.getUserId())){
      return error("请选择账户");
    }
    Account account1_ =  repository.findOne(account.getUserId());
    if (Objects.nonNull(account1_)){
      return error("请选择其他账户");
    }
    Date date = new Date();
    account.setCreatedTime(date);
    account.setModifiedTime(date);
    repository.save(account);
    return ResultGenerator.ok();
  }
  @Transactional
  @DeleteMapping
  public Result<String> delete(@RequestBody Long[] ids) {
    Arrays.asList(ids).forEach(repository::delete);
    return ok();
  }
  @Transactional
  @PutMapping("/{userId}")
  public Result<String> update(@PathVariable("userId") Account old, @RequestBody Account account) {
    if (Objects.isNull(old)) {
      return error();
    }
    String payType = account.getPayType();
    System.out.println("payType:"+payType);
//    old.setRoleSet(account.getRoleSet());
    updateVal(old, account);
    repository.save(old);
    return ok();
  }
  @GetMapping("/{userId}")
  public Result<Account> get(@PathVariable("userId") Account account) {
    return ok(account);
  }

  @GetMapping
  public Result<Page<Account>> getAll(Account account, Pageable pageable) {
    return ok(repository.findAll(whereSpec(account), pageable));
  }

  private Specification<Account> whereSpec(final Account account) {
    return (root, query, cb) -> {
      List<Predicate> predicates = new ArrayList<>();
      if (StringUtils.isNotEmpty(account.getName())) {
        predicates.add(cb.like(root.get("name"), "%" + account.getName() + "%"));  //根据登录名称模糊查询
      }
      return cb.and(predicates.toArray(new Predicate[predicates.size()]));
    };
  }
  public void updateVal(Account old, Account account) {
    Date now = new Date();
    old.setModifiedTime(now);
    old.setAppId(val(old::getAppId,account::getAppId));
    old.setAppKey(val(old::getAppKey,account::getAppKey));
    old.setName(val(old::getName,account::getName));
    old.setPayType(val(old::getPayType,account::getPayType));
    old.setRemark(val(old::getRemark,account::getRemark));
  }
  public <T> T val(Supplier<T> oldVal, Supplier<T> newVal) {
    T oldV = oldVal.get();
    T newV = newVal.get();
    return Objects.nonNull(newV) ? newV : oldV;
  }
}
